package tech.luoyu.ki.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zengyingying
 * @date 2016年5月10日 下午3:57:19
 * @Description 方法描述：
 * 		isEmpty(String str) 判断字符串是否为空
 * 		getLength(String str) 获取字符串长度
 * 		getCurrentTime(String pattern) 获取当前时间
 * 		String getCurrentTime() 获取当前时间 默认格式为“yyyy/M/d HH:mm:ss”
 */

public class StringUtil {

	public static String nullToEmpty(String str) {
		if (str == null) {
			return "";
		} else {
			return str;
		}
	}

	public static String nullToTrimEmpty(String str) {
		if (str == null) {
			return "";
		} else {
			return str.trim();
		}
	}

	public static boolean isEmpty(String str) {
		return str == null || str.equals("") || str.equalsIgnoreCase("null");
	}

	public static boolean isEmpty(Object obj) {
		if (obj == null) {
			return true;
		}
		if ("".equals(obj)) {
			return true;
		}
		if ("".equals(obj.toString().trim())) {
			return true;
		}
		if ("null".equalsIgnoreCase(obj.toString().trim())) {
			return true;
		}
		return false;
	}

	public static boolean isTrimEmpty(String str) {
		return str == null || str.trim().equals("") || str.trim().equalsIgnoreCase("null");
	}

	public static boolean isNull(String str) {
		return str == null;
	}

	public static int getLength(String str) {
		int length = 0;
		if (str == null || str.equals("")) {
			return length;
		}
		for (int i = 0; i < str.length(); i++) {
			int ascii = Character.codePointAt(str, i);
			if (ascii >= 0 && ascii <= 255) {
				length++;
			} else {
				length += 2;
			}
		}
		return length;
	}

	public static String getCurrentTime(String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(new Date());
	}

	public static String getCurrentDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date());
	}

	public static String getCurrentTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date());
	}

	public static String getCurrentTimeMillis() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
		return sdf.format(new Date());
	}

	public static String formatTime(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}

	public static String formatDate(String string) {
		String startDate = "";
		try {
			if (!isEmpty(string)) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
				String str = sdf2.format(new Date());
				Date date = sdf.parse(str + " " + string);
				startDate = sdf.format(date);
			}
		} catch (Exception e) {
			// e.printStackTrace();
		}
		return startDate;
	}

	public static String formatDateToString(String string) {
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (!isEmpty(string)) {
			try {
				return sd.format(sd.parse(string));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return "";
	}

	public static Date parseDate(String string) {
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (!isEmpty(string)) {
			try {
				return sd.parse(string);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
